package com.example.huanhang.changzhouapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;

import com.example.huanhang.changzhouapp.R;
import com.example.huanhang.changzhouapp.adapter.PdfAdapter;
import com.example.huanhang.changzhouapp.bean.Pdf;
import com.example.huanhang.changzhouapp.util.ThreadUtil;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ListActivity extends AppCompatActivity {
    private ProgressBar mMainProgress;
    private RecyclerView mMainList;
    private List<Pdf> pdfs = new ArrayList<>();
    private PdfAdapter pdfAdapter;
    private SmartRefreshLayout mMainRefresh;
    private String mFl_name;
    private AppCompatTextView mTvTitle;
    private AppCompatImageView mIvBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        initView();
    }

    private void initView() {
        mFl_name = getIntent().getStringExtra("fl_name");
        mMainRefresh = (SmartRefreshLayout) findViewById(R.id.main_refresh);
        mMainProgress = (ProgressBar) findViewById(R.id.main_progress);
        mTvTitle = (AppCompatTextView) findViewById(R.id.tv_title);
        mIvBack = (AppCompatImageView) findViewById(R.id.iv_back);
        mMainList = (RecyclerView) findViewById(R.id.main_list);
        mTvTitle.setText(mFl_name);
        mMainProgress.setVisibility(View.VISIBLE);
        mMainList.setLayoutManager(new LinearLayoutManager(this));
        pdfAdapter = new PdfAdapter(R.layout.item_pdfinfo, pdfs);
        pdfAdapter.setOnItemClickListener((adapter, view, position) -> {
            Pdf foods = pdfs.get(position);
            Intent intent = new Intent(ListActivity.this, DetailsActivity.class);
            Bundle bundle = new Bundle();
            bundle.putParcelable("pdfValues", foods);
            intent.putExtras(bundle);
            startActivity(intent);
        });
        mMainList.setAdapter(pdfAdapter);
        mMainRefresh.setEnableLoadMore(true);
        mMainRefresh.setRefreshHeader(new ClassicsHeader(this));
        mMainRefresh.setOnRefreshListener(refreshLayout -> {
            getDataForAdapter();
        });
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ImmersionBar.with(this).statusBarColor(R.color.title).init();
        initData();
    }


    private void getDataForAdapter() {
        if (pdfs.size() != 0) {
            pdfs.clear();
        }
        requestNet();


    }

    private void initData() {
        mMainRefresh.autoRefresh();
    }

    private void requestNet() {
        ThreadUtil.runOnBackThread(() -> {
            final String sql = "select lccb_mc,lccb_bh,lccb_fl,lccb_path,DATE_FORMAT(lccb_time,'%Y-%m-%d') AS 'time'" +
                    "  FROM basic WHERE  lccb_fl='" + mFl_name + "' and SUBSTRING(lccb_path, -3)='pdf'";
            try {
                ResultSet resultSet = ThreadUtil.result(sql);
                while (resultSet.next()) {
                    Pdf pdf = new Pdf();
                    pdf.setName(resultSet.getString("lccb_mc"));
                    pdf.setFenlei(resultSet.getString("lccb_fl"));
                    pdf.setNumber(resultSet.getString("lccb_bh"));
                    pdf.setPzDate(resultSet.getString("time"));
                    pdf.setShiSDate(resultSet.getString("time"));
                    pdf.setPdfPath(resultSet.getString("lccb_path"));
                    pdfs.add(pdf);
                }
                querySuccess();
                resultSet.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    private void querySuccess() {
        ThreadUtil.runOnUiThread(() -> {
            if (pdfs.size() == 0) {
                mMainProgress.setVisibility(View.GONE);
                mMainRefresh.finishRefresh();
                return;
            }
            pdfAdapter.notifyDataSetChanged();
            mMainProgress.setVisibility(View.GONE);
            mMainRefresh.finishRefresh();
        });
    }
}
